package com.woniuxy.provider.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.woniuxy.commons.annotation.RequirePerms;
import com.woniuxy.commons.entity.Goods;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @Author: yangtao
 * @date 2021/8/2 14:31
 * @Version 1.0
 */
@RestController
@RequestMapping("/goods")
public class GoodsController {
    //dashboard只会监控使用了HystrixCommand的方法
    @HystrixCommand(fallbackMethod = "abc")
    @GetMapping("/all")
    @RequirePerms("goods:all")
    public List<Goods> all() throws InterruptedException {
        /*Thread.sleep(2000);*/
        return Arrays.asList(
                new Goods(1001,"phone",1000),
                new Goods(1002,"phone1",1000),
                new Goods(1003,"phone2",1000),
                new Goods(1004,"phone3",1000)
        );
    }

    public List<Goods> abc() {
        /*Thread.sleep(2000);*/
        return Arrays.asList(
                new Goods(00000,"服务器正忙，请稍后再试",0)
        );
    }

    @GetMapping("/find/{id}")
    public Goods  findById(@PathVariable("id") int id){
        /*try {
            Thread.sleep(5000);  //处理时间长，服务压力大
        } catch (InterruptedException e) {
            e.printStackTrace();
        }*/
        System.out.println(id);
    return new Goods(1004,"mouse",1000);
    }

    @PostMapping("/add")
    public boolean add(@RequestBody Goods goods){
        System.out.println(goods);
        return true;
    }

    @PutMapping("/update")
    public boolean update(@RequestBody Goods goods){
        System.out.println(goods);
        return true;
    }

    @DeleteMapping("/del/{id}")
    public void del(@PathVariable("id") int id){
        System.out.println(id);
    }
}




















